import java.util.*;

public class Leetcode {
}

//leetcode:1047:删除字符中所有相邻重复项
class Solution1 {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        StringBuilder sb = new StringBuilder();
        stack.push(s.charAt(0));
        for(int i = 1;i<s.length();i++){
            if(!stack.isEmpty() && stack.peek().equals(s.charAt(i))){
                stack.pop();
            }else{
                stack.push(s.charAt(i));
            }
        }
        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }
        return sb. reverse().toString();
    }
}

//leetcode:844:比较含退格的字符串
class Solution2 {
    public boolean backspaceCompare(String s, String t) {
        return build(s).equals(build(t));
    }

    public String build(String str) {
        StringBuffer ret = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i) != '#') {
                ret.append(str.charAt(i));
            }else {
                if (ret.length() > 0) {
                    ret.deleteCharAt(ret.length()-1);
                }
            }
        }
        return ret.toString();
    }
}